/**
 * district 模型
 */

'use strict';

var mongodb = require('../mongodb.js');
var Schema = mongodb.Schema;

// create a schema
var ModelSchema = new Schema({
    adCode: Number,
    areaCode: Number,
    cityCode: Number,
    name: String,
    loc: {
        'type': {
            type: String,
            enum: "Point",
            default: "Point"
        },
        coordinates: {
            type: [Number],
            default: [0, 0]
        }
    },
    pId: {type: Schema.Types.ObjectId, index: 1},
    subId: [{
        type: Schema.Types.ObjectId,
        ref: 'district'
    }],
    level: String, // province / city / district ... 
    createdAt: Date,
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

ModelSchema.pre('save', function (next) {
    if (!this.createdAt) {
        this.createdAt = new Date();
    }
    next();
});
ModelSchema.index({
    location: '2dsphere'
});

// 指定collection name，默认是复数
var district = mongodb.model('district', ModelSchema, 'district');
exports = module.exports = district;